﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using MainFrameDesign.ContentContorl;


namespace MainFrameDesign
{
    public partial class DesignControl : UserControl
    {
        public Dom Root = new Dom();
        public event System.Windows.Forms.DragEventHandler DesignDragEnter;
        public event System.Windows.Forms.DragEventHandler DesignDragDrop;
        public event System.Windows.Forms.DragEventHandler DesignDragOver;
        public event EventHandler DesignDragLeave;
        public DesignControl()
        {
            InitializeComponent();
            MyScroll.Minimum = 0;
            MyScroll.Maximum = 0;
            MyScroll.Value = 0;
            Root.OnHeightChange += root_OnScroll;
        }

        void root_OnScroll(int height)
        {
            if (height > 0)
            {
                if (!MyScroll.Enabled)
                {
                    MyScroll.Enabled = true;
                }
                MyScroll.Maximum = height+(MyScroll.LargeChange-1);
            }
            else 
            {
                if (MyScroll.Enabled)
                {
                    MyScroll.Enabled = false;
                    MyScroll.Maximum = 0;
                }
            }
        }

        private void MyScroll_ValueChanged(object sender, EventArgs e)
        {
            DrawPanel.Invalidate();
        }

        private void DrawPanel_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            g.Clear(Color.White);
            g.TranslateTransform(0, -MyScroll.Value);
            Root.Draw(g);
        }

        private void DrawPanel_Resize(object sender, EventArgs e)
        {
            if (DrawPanel.Width != 0 && DrawPanel.Height != 0)
            {
                Root.Resize(DrawPanel.Width, DrawPanel.Height);
                DrawPanel.Invalidate();
            }
        }

        private void DrawPanel_DragDrop(object sender, DragEventArgs e)
        {
            if (DesignDragDrop != null) 
            {
                DesignDragDrop(sender, e);
            }
        }

        private void DrawPanel_DragEnter(object sender, DragEventArgs e)
        {
            if (DesignDragEnter != null)
            {
                DesignDragEnter(sender, e);
            }
        }

        private void DrawPanel_DragOver(object sender, DragEventArgs e)
        {
            if (DesignDragOver != null)
            {
                DesignDragOver(sender, e);
            }
        }

        private void DrawPanel_DragLeave(object sender, EventArgs e)
        {
            if (DesignDragLeave != null)
            {
                DesignDragLeave(sender, e);
            }
        }

        public int GetScrollValue() 
        {
            if (MyScroll.Enabled == false)
                return 0;
            return MyScroll.Value;
        }

        public void InvalidateDesign() 
        {
            this.DrawPanel.Invalidate();
        }
    }
}
